package com.longrise.android.byjk.plugins.hra.hratabfirst;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.CourseDefaultItem;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5.
 */

public class HraPlacesPresenter extends HraPlacesContract.Presenter{


    private List<OrderPlaceDefaultItem> mParentBeanList = new ArrayList<>();

    @Override
    protected void init() {

    }


    //获取可预约时间段,库存
    @Override
   public void getTimeslotData(String storeId,String bespeakdate) {

        EntityBean bean = new EntityBean();
        bean.set("storeid", storeId);
        bean.set("bespeakdate", bespeakdate);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_getTimeslot", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        parseDate(bean);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    /**
     * 解析可预约时间段,库存
     *
     * @param
     */
    public void parseDate(EntityBean bean) {
        mParentBeanList.clear();
        EntityBean[] dataBean = bean.getBeans("result");
        if (dataBean != null) {
            for (EntityBean resultbean : dataBean) {
                String timeslot = resultbean.getString("timeslot");
                String stockpeople = resultbean.getString("stockpeople");
                String timeslotid = resultbean.getString("id");
                String machineid = resultbean.getString("machineid");
                String bespeakdate = resultbean.getString("bespeakdate");
                String price = resultbean.getString("price");
                String machinemodelname = resultbean.getString("machinemodelname");
                String machinemodel = resultbean.getString("machinemodel");
                OrderPlaceChildBean childBean = new OrderPlaceChildBean();
                childBean.setTimeslot(timeslot);
                childBean.setStockpeople(stockpeople);
                childBean.setTimeslotid(timeslotid);
                childBean.setMachineid(machineid);
                childBean.setBespeakdate(bespeakdate);
                childBean.setPrice(price);
                childBean.setMachineType(machinemodelname);
                childBean.setMachinemodel(machinemodel);

                OrderPlaceDefaultItem item = new OrderPlaceDefaultItem(CourseDefaultItem.TYPE2);
                item.setOrderPlaceBean(childBean);
                mParentBeanList.add(item);
            }
            mView.refreshStockData(mParentBeanList);
        } else {

        }

    }
}
